﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ProjectZhuangControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            Bind();
        }
        
        protected void Bind()
        {
            Button search = (Button)FindControl("search");
            Button Submit1 = (Button)FindControl("Submit1");
            Button add = (Button)FindControl("add");
            search.Click += new EventHandler(Go_Search);
            Submit1.Click += new EventHandler(Go_Back);
            add.Click += new EventHandler(Btn_Click);
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            DataTable dt = _EluZSDataProvider.GetProZhuang(HttpContext.Current.Request["pid"], GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CustomGridList1.PreRender += new EventHandler(CustomGridList1_PreRender);
            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                LinkButton LinkButton1 = (LinkButton)gvr.FindControl("LinkButton1");
                LinkButton1.Click += new EventHandler(Delete_Zhuang_Click);
            }
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridList1_PreRender(object sender, EventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            string query = HttpContext.Current.Request["query"];
            if (!string.IsNullOrEmpty(query))
            {
                if (query == "loupan")
                {
                    CustomGridList1.Columns[8].Visible = true;
                }
                else if (query == "cqzh")
                {
                    CustomGridList1.Columns[9].Visible = true;
                }
                else if (query == "hbhbc")
                {
                    CustomGridList1.Columns[10].Visible = true;
                }
            }
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Btn_Click(object sender, EventArgs e)
        {
            string pid = HttpContext.Current.Request["PID"].ToString();
            string url = string.Empty;
            string query = HttpContext.Current.Request["query"];
            if (query == "hbhbc")
            {
                url = string.Format("HuoBiHuaBuChangXieYi.aspx?action=insert&PID={0}", pid);
            }
            else
            {
                url = string.Format("EditZhuang.aspx?action=insert&PID={0}&query={1}", pid, query);
            }
            HttpContext.Current.Response.Redirect(url);
        }
        protected void Go_Back(object sender, EventArgs e)
        {
            string temp = HttpContext.Current.Request["query"];
            if (!string.IsNullOrEmpty(temp))
            {
                if (temp == "cqzh")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanProList.aspx?query=cqzh");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ZhengShouAnZhiFang.aspx");
                }
            }
            else
            {
                HttpContext.Current.Response.Redirect("ZhengShouAnZhiFang.aspx");
            }
        }
        protected void Delete_Zhuang_Click(object sender, EventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            LinkButton lBtn = sender as LinkButton;
            GridViewRow gvr = lBtn.Parent.Parent as GridViewRow;
            string ZID = CustomGridList1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            if (!_EluZSDataProvider.CheckData(HttpContext.Current.Request["PID"],ZID))
            {
                bool del = _EluZSDataProvider.DeleteXmByID("zs_prozhuang","ID", ZID);
                if (del)
                {
                    _EluZSDataProvider.DeleteXmByID("zs_proloupan", "ID", ZID);
                    HttpContext.Current.Response.Redirect("ProjectZhuang.aspx?PID=" + HttpContext.Current.Request["PID"] + "&query=loupan");
                }
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('此幢中存在已签署协议房间！');window.location.href=location.href;</script>");
            }
        }
        protected string GetSqlWhere() {
            string strWhere = string.Empty;
            TextBox dkh = (TextBox)FindControl("dkh");
            TextBox zh = (TextBox)FindControl("zh");
            if (!string.IsNullOrEmpty(dkh.Text.Trim()))
            {
                strWhere += string.Format(" and 地块号 like '%{0}%'", dkh.Text.Trim());
            }
            if (!string.IsNullOrEmpty(zh.Text.Trim()))
            {
                strWhere += string.Format(" and 幢号 like '%{0}%'", zh.Text.Trim());
            }
            return strWhere;
        }
    }
}


